<?php
	/* ******************************************************** */
	/* Arcans Project (2010)                                    */
	/* Jeu sous forme d'application web entièrement conçu       */
	/* et réalisé par Killian Le Maitre                         */
	/* Jeu sous licence GNU GPL v3 (voir NOTICE et COPYING)     */
	/* ******************************************************** */

	//**********************
	//classe de réponse ajax
	//**********************
	class AjaxRepsonseClass {
		//propriétés :
		private $result;
		//méthodes :
		function __construct($session) {
			switch ($session->actPage()) {
				//case de l'inscription, première partie **********************************************
				case "ains" :
					$info = htmlentities($_POST["info"]);
					$mysqlins = new MysqlClass(0);
					$resins = mysql_fetch_array($mysqlins->resultFun("select count(*) as 'users' from users where users='".$info."'"));
					$this->result = $resins["users"];
					$resins = mysql_fetch_array($mysqlins->resultFun("select count(*) as 'unvalid_users' from unvalid_users where users='".$info."'"));
					$this->result = $this->result + $resins["unvalid_users"];
					break;
				//cas de l'inscription, deuxième partie ***********************************************
				case "ains2" :
					$infos["pseudo"] = htmlentities($_POST["info1"]);
					$infos["email"] = htmlentities($_POST["info2"]);
					$pwd_tab_alp = array ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
					$pwd_tab = array_rand($pwd_tab_alp, 8);
					$pwd = "";
					foreach ($pwd_tab as $elt) $pwd .= $pwd_tab_alp[$elt];
					$code = hash("sha256", mt_rand());
					//enregistrement en base de données
					$mysqlins2 = new MysqlClass(1);
					$mysqlins2->resultFun('insert into unvalid_users (users,password,email,code) values("'.$infos["pseudo"].'","'.$pwd.'","'.$infos["email"].'","'.$code.'")');
					//envoi email
					$element = array ("ains2",$infos["pseudo"],$pwd,$code);
					mailSend($infos["email"],"Arcans Project : inscription",$element);
					break;
				//cas de la connexion *****************************************************************
				case "acon" :
					$infos["pseudo"] = htmlentities($_POST["info1"]);
					$infos["pwd"] = htmlentities($_POST["info2"]);
					$mysqlcon = new MysqlClass(0);
					$res = mysql_fetch_array($mysqlcon->resultFun("select count(*) as 'find' from users where users='".$infos["pseudo"]."'"));
					if ($res["find"] == 1) {
						$hash = md5(hash("sha256", $infos["pseudo"]) . $infos["pwd"]);
						$res = mysql_fetch_array($mysqlcon->resultFun("select count(*) as 'find' from users where password='".$hash."'"));
						if ($res["find"] == 1) {
							$info = mysql_fetch_array($mysqlcon->resultFun("select * from users where password='".$hash."'"));
							$_SESSION["pseudo"] = $info["users"];
							$_SESSION["level"] = $info["level"];
							$mysqlclean = new MysqlClass(2);
							$date = date("Y-m-d H:i:s",time() - (7*24*60*60));
							$mysqlclean->resultFun("delete from unvalid_users where time<'".$date."'");
							$this->result = $res["find"];
						}
						else $this->result = 0;
					}
					else $this->result = 2;
					break;
				//cas de l'oubli du mot de passe ******************************************************
				case "afpwd" :
					$infos["pseudo"] = htmlentities($_POST["info1"]);
					$infos["email"] = htmlentities($_POST["info2"]);
					$mysqlfpwd = new MysqlClass(0);
					$res = mysql_fetch_array($mysqlfpwd->resultFun("select count(*) as 'find' from users where users='".$infos["pseudo"]."' and email='".$infos["email"]."'"));
					if ($res["find"] == 1) {
						//modification de la base de données
						$pwd_tab_alp = array ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
						$pwd_tab = array_rand($pwd_tab_alp, 8);
						$pwd = "";
						foreach ($pwd_tab as $elt) $pwd .= $pwd_tab_alp[$elt];
						$code = md5(hash("sha256", $infos["pseudo"]) . hash("sha256", $pwd));
						$mysqlfpwdmod = new MysqlClass(2);
						$mysqlfpwdmod->resultFun("update users set password='".$code."' where users='".$infos["pseudo"]."'");
						//envoi du mail
						mailSend($infos["email"],"Arcans Project : oubli du mot de passe",array ("fpwd",$infos["pseudo"],$pwd));
						//fin de la requete
						$this->result = 1;
					}
					else $this->result = 0;
					break;
				//cas du renvoi du mail pour l'inscription, première partie ***************************
				case "amailv1" :
					$infos["pseudo"] = htmlentities($_POST["info"]);
					$mysqlamv1 = new MysqlClass(0);
					$res = mysql_fetch_array($mysqlamv1->resultFun("select count(*) as 'find' from unvalid_users where users='".$infos["pseudo"]."'"));
					$this->result = $res["find"];
					break;
				//cas du renvoi du mail pour l'inscription, deuxième partie ***************************
				case "amailv2" :
					$infos["pseudo"] = htmlentities($_POST["info1"]);
					$infos["email"] = htmlentities($_POST["info2"]);
					$mysqlamv2 = new MysqlClass(0);
					$res = mysql_fetch_array($mysqlamv2->resultFun("select count(*) as 'find' from unvalid_users where users='".$infos["pseudo"]."' and email='".$infos["email"]."'"));
					if ($res["find"] == 1) {
						//modification de la base de données
						$pwd_tab_alp = array ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
						$pwd_tab = array_rand($pwd_tab_alp, 8);
						$pwd = "";
						foreach ($pwd_tab as $elt) $pwd .= $pwd_tab_alp[$elt];
						$code = hash("sha256", mt_rand());
						//enregistrement en base de données
						$mysqlmlm = new MysqlClass(2);
						$mysqlmlm->resultFun('delete from unvalid_users where users="'.$infos["pseudo"].'"'); 
						$mysqlmlm->resultFun('insert into unvalid_users (users,password,email,code) values("'.$infos["pseudo"].'","'.$pwd.'","'.$infos["email"].'","'.$code.'")');
						//envoi du mail
						$element = array ("renvoi_ins",$infos["pseudo"],$pwd,$code);
						mailSend($infos["email"],"Arcans Project : inscription, renvoi",$element);
						//fin de la requête ajax, envoi de la réponse
						$this->result = 1;
					}
					else $this->result = 0;
					break;
				//cas de la déconnexion ***************************************************************
				case "adecon" : $_SESSION = array(); session_destroy(); break;
				//cas de la page de contact ***********************************************************
				case "acntt" :
					$titre = htmlentities($_POST["info1"]);
					$sender = htmlentities($_POST["info2"]);
					$texte = htmlentities($_POST["info3"]);
					//envoi du mail
					mailSend("arcans.project@gmail.com","Arcans Project : message de contact",array ("contact",$titre,$sender,$texte));
					break;
				//cas de la modification du pseudonyme ************************************************
				case "acplogv" :
					$pseudoa = htmlentities($_POST["info1"]);
					$pwd = htmlentities($_POST["info2"]);
					$pseudon = htmlentities($_POST["info3"]);
					if ($pseudoa == $_SESSION["pseudo"]) {
						$mysqlcplog1 = new MysqlClass(0);
						$res = mysql_fetch_array($mysqlcplog1->resultFun("select count(*) as 'find' from users where users='".$pseudoa."' and password='".md5(hash("sha256", $pseudoa) . $pwd)."'"));
						if ($res["find"] == 1) {
							//inscription en bdd
							$res = mysql_fetch_array($mysqlcplog1->resultFun("select id from users where users='".$_SESSION["pseudo"]."'"));
							$mysqlcplog2 = new MysqlClass(2);
							$mysqlcplog2->resultFun("update users set users='".$pseudon."',password='".md5(hash("sha256",$pseudon).$pwd)."' where id=".$res["id"]);
							$res = mysql_fetch_array($mysqlcplog1->resultFun("select email from users where id=".$res["id"]));
							//envoi du mail
							mailSend($res["email"],"Arcans Project : Modification du pseudonyme",array ("pseudomod",$pseudon));
							//fin de changement
							$_SESSION["pseudo"] = $pseudon;
							$this->result = 1;
						}
						else $this->result = 0;
					}
					else $this->result = 0;
					break;
				//cas de la modification de l'email ***************************************************
				case "amailmod" :
					$pseudo = htmlentities($_POST["info1"]);
					$pwd = htmlentities($_POST["info2"]);
					$emailn = htmlentities($_POST["info3"]);
					if ($pseudo == $_SESSION["pseudo"]) {
						$mysqlmail = new MysqlClass(0);
						$res = mysql_fetch_array($mysqlmail->resultFun("select count(*) as 'find' from users where users='".$pseudo."' and password='".md5(hash("sha256", $pseudo) . $pwd)."'"));
						if ($res['find'] == 1) {
							//enregistrement des nouvelles données
							$mysqlmailn2 = new MysqlClass(2);
							$mysqlmailn2->resultFun("update users set email='".$emailn."' where users='".$pseudo."'");
							//envoi du mail
							mailSend($emailn,"Arcans Project : Modification de l'adresse électronique",array("mailmod",$pseudo,$emailn));
							//fin de la requête
							$this->result = 1;
						}
						else $this->result = 0;
					}
					else $this->result = 0;
					break;
				//cas de la modification du mot de passe **********************************************
				case "apwdmod" :
					$pwdn1 = htmlentities($_POST["info1"]);
					$pwdn2 = htmlentities($_POST["info2"]);
					$pseudo = htmlentities($_POST["info3"]);
					$pwda = htmlentities($_POST["info4"]);
					if ($pseudo == $_SESSION["pseudo"]) {
						$mysqlpwdmod = new MysqlClass(0);
						$res = mysql_fetch_array($mysqlpwdmod->resultFun("select count(*) as 'find' from users where users='".$pseudo."' and password='".md5(hash("sha256", $pseudo) . $pwda)."'"));
						if ($res["find"] == 1) {
							//enregistrement des nouvelles données
							$mysqlpwdmod2 = new MysqlClass(2);
							$mysqlpwdmod2->resultFun("update users set password='".md5(hash("sha256", $pseudo) . $pwdn1)."' where users='".$pseudo."'");
							//envoi du mail
							$res = mysql_fetch_array($mysqlpwdmod->resultFun("select email from users where users='".$pseudo."'"));
							mailSend($res["email"],"Arcans Project : Modification du mot de passe",array("passmod",$pseudo,$pwdn2));
							//fin de la requête
							$this->result = 1;
						}
						else $this->result = 0;
					}
					else $this->result = 0;
					break;
				//cas de la suppression du compte *****************************************************
				case "asupp" :
					$pseudo = htmlentities($_POST["info1"]);
					$pwd = htmlentities($_POST["info2"]);
					if ($pseudo == $_SESSION["pseudo"]) {
						$mysqlsupp = new MysqlClass(0);
						$res = mysql_fetch_array($mysqlsupp->resultFun("select count(*) as 'find' from users where users='".$pseudo."' and password='".md5(hash("sha256", $pseudo).$pwd)."'"));
						if ($res["find"] == 1) {
							//récupération des données
							$code = hash("sha256", mt_rand());
							$res = mysql_fetch_array($mysqlsupp->resultFun("select email from users where users='".$pseudo."'"));
							//enregistrement dans unvalid, et suppression dans users
							$mysqlsupp2 = new MysqlClass(2);
							$mysqlsupp2->resultFun("insert into unvalid_users (users,password,email,code) values('".$pseudo."','','".$res["email"]."','".$code."')");
							$mysqlsupp2->resultFun("delete from users where users='".$pseudo."'");
							//envoi du mail
							mailSend($res["email"],"Arcans Project : Suppression de compte",array("suppression",$pseudo,$code));
							//déconnexion
							$_SESSION = array();
							session_destroy();
							//fin de la requête
							$this->result = 1;
						}
						else $this->result = 8;
					}
					else $this->result = 0;
					break;
			}
		}
		public function affichage() { echo $this->result; }
	}
?>